<?php

use App\Http\Controllers\QH_Api\ArticleController;
use App\Http\Controllers\QH_Api\CourseColumnController;
use App\Http\Controllers\QH_Api\DocumentController;
use App\Http\Controllers\QH_Api\GroupBuyController;
use App\Http\Controllers\QH_Api\LearnVideoController;
use App\Http\Controllers\QH_Api\NetCourseController;
use App\Http\Controllers\QH_Api\NoticeController;
use App\Http\Controllers\QH_Api\QuestionController;
use App\Http\Controllers\QH_Api\QuestionLibraryController;
use App\Http\Controllers\QH_Api\SchoolController;
use App\Http\Controllers\TK_Api\AuthController;
use App\Http\Controllers\TK_Api\CommentController;
use App\Http\Controllers\TK_Api\CommonController;
use App\Http\Controllers\TK_Api\CourseController;
use App\Http\Controllers\TK_Api\KnowledgeController;
use App\Http\Controllers\TK_Api\NoteController;
use App\Http\Controllers\TK_Api\OrderController;
use App\Http\Controllers\TK_Api\PaperController;
use App\Http\Controllers\TK_Api\QuestionsController;
use App\Http\Controllers\TK_Api\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

/**
 * 题库微信小程序接口
 */
Route::prefix('tk_mp')->group(function () {
    Route::post('auth/login', [AuthController::class, 'login']);

    // 惰性鉴权
    Route::get('course', [CourseController::class, 'list']);
    Route::get('course/switch', [CourseController::class, 'switch']);
    Route::get('common/init', [CommonController::class, 'init']);
    Route::any('order/wechatNotify', [OrderController::class, 'wechatNotify']);  // 微信支付结果通知

    /**
     * 需要登录
     */
    Route::middleware(['auth:api', 'checkUserStatus'])->group(function () {

        // 通用
        Route::prefix('common')->group(function () {
            Route::get('getRank', [CommonController::class, 'getRank']);
            Route::post('feedback', [CommonController::class, 'feedbackStore']);
            Route::post('uploadFile', [CommonController::class, 'uploadFile']);
        });

        // 学科
        Route::prefix('course')->group(function () {
            Route::get('questions', [CourseController::class, 'getQuestionIds']);
            Route::post('useCdkey', [CourseController::class, 'useCdkey']);
        });

        // 知识点
        Route::prefix('knowledge')->group(function () {
            Route::get('', [KnowledgeController::class, 'list']);
            Route::get('questions', [KnowledgeController::class, 'questions']);
            Route::get('posts/{id}', [KnowledgeController::class, 'getPosts']);
            Route::get('post/{id}', [KnowledgeController::class, 'getPost']);
            Route::post('postCollect/{id}', [KnowledgeController::class, 'postCollect']);
        });

        // 历年试卷
        Route::prefix('lnsj')->group(function () {
            Route::get('', [PaperController::class, 'list']);
        });

        // 用户
        Route::prefix('user')->group(function () {
            Route::get('getInfo', [UserController::class, 'getInfo']);
            Route::post('updateInfo', [UserController::class, 'updateInfo']);
            Route::get('getCollect', [UserController::class, 'getCollect']);
            Route::get('getCuoti', [UserController::class, 'getCuoti']);
            Route::delete('delCollect', [UserController::class, 'delCollect']);
            Route::delete('delCuoti', [UserController::class, 'delCuoti']);
            Route::delete('delKnowledgePost', [UserController::class, 'delKnowledgePost']);
            Route::get('statistics', [UserController::class, 'statistics']);
            Route::get('getNote', [NoteController::class, 'getMyNote']);
            Route::get('getCollectKnowledgePost', [UserController::class, 'getCollectKnowledgePost']);
        });

        // 订单
        Route::prefix('order')->group(function () {
            Route::get('', [OrderController::class, 'list']);
            Route::post('payment', [OrderController::class, 'getOrderInfo']);
            Route::post('submit', [OrderController::class, 'orderSubmit']);
        });

        // 题目
        Route::prefix('questions')->group(function () {
            Route::get('', [QuestionsController::class, 'list']);
            Route::post('init', [QuestionsController::class, 'init']);
            Route::post('collect', [QuestionsController::class, 'collect']);
            Route::post('report', [QuestionsController::class, 'report']);
            Route::post('check', [QuestionsController::class, 'resultCheck']);
            Route::get('getNoteByQuestion/{id}', [NoteController::class, 'getNoteByQuestionId']);
            Route::get('getCommentByQuestion/{id}', [CommentController::class, 'getCommentByQuestionId']);
        });

        // 笔记
        Route::prefix('note')->group(function () {
            Route::post('', [NoteController::class, 'store']);
            Route::delete('{id}', [NoteController::class, 'delete']);
            Route::post('like/{id}', [NoteController::class, 'like']);
        });

        // 评论
        Route::prefix('comment')->group(function () {
            Route::post('', [CommentController::class, 'store']);
            Route::delete('{id}', [CommentController::class, 'delete']);
            Route::post('like/{id}', [CommentController::class, 'like']);
        });
    });
});


/**
 * 启航微信小程序接口
 */
Route::prefix('qh_mp')->middleware(['api_pre'])->group(function () {
    Route::post('auth/login', [AuthController::class, 'login']);

    Route::get('common/getProvinceList', [\App\Http\Controllers\QH_Api\CommonController::class, 'getProvinceList']);
    Route::get('common/getHomeConfig', [\App\Http\Controllers\QH_Api\CommonController::class, 'getHomeConfig']);
    Route::get('common/getWangkeConfig', [\App\Http\Controllers\QH_Api\CommonController::class, 'getWangkeConfig']);
    Route::get('common/getKaoshiConfig', [\App\Http\Controllers\QH_Api\CommonController::class, 'getKaoshiConfig']);
    Route::get('common/init', [\App\Http\Controllers\QH_Api\CommonController::class, 'getInitConfig']);
    Route::post('common/uploadFile', [CommonController::class, 'uploadFile']);
    Route::post('common/uploadFile2', [CommonController::class, 'uploadFile2']);
    Route::get('common/getRank', [\App\Http\Controllers\QH_Api\CommonController::class, 'getRankV2']);
    Route::get('common/getKaoshiRank', [\App\Http\Controllers\QH_Api\CommonController::class, 'getKaoshiRank1']);

    // 获取知识点下的试题初始化数据
    Route::get('getQuestionsByKnowledge', [QuestionController::class, 'getQuestionsByKnowledge']);
    Route::get('getQuestionsByCuoti', [QuestionController::class, 'getQuestionsByCuoti']);
    Route::get('getQuestionsByWrongOrCollect', [QuestionController::class, 'getQuestionsByWrongOrCollect']);
    Route::get('getQuestionsByCollect', [QuestionController::class, 'getQuestionsByCollect']);

    // 获取知识点下试题的答题结果
    Route::get('getQuestionsResultByKnowledge', [QuestionController::class, 'getQuestionsResultByKnowledge']);

    // 根据ids获取试题详情
    Route::get('getQuestionsByIds', [QuestionController::class, 'getQuestionsByIds']);

    // 题库详情
    Route::get('questionLibrary/{id}', [QuestionLibraryController::class, 'show']);
    // 学科详情
    Route::get('course/{id}', [\App\Http\Controllers\QH_Api\CourseController::class, 'show']);

    // 获取知识点子级（小节）
    Route::get('getKnowledge2/{id}', [\App\Http\Controllers\QH_Api\CourseController::class, 'getKnowledge2']);

    // 公告
    Route::prefix('notice')->group(function () {
        Route::get('', [NoticeController::class, 'list']);
        Route::get('{id}', [NoticeController::class, 'show']);
    });

    Route::prefix('school')->group(function () {
        Route::get('getHomeConfig', [SchoolController::class, 'index']);
        Route::get('getSchoolList', [SchoolController::class, 'schoolList']);
        Route::get('getSchool/{id}', [SchoolController::class, 'school']);
        Route::get('getSchoolSpecList', [SchoolController::class, 'getSchoolSpecList']);
        Route::get('getSchoolSpecList2', [SchoolController::class, 'getSchoolSpecList2']);
        Route::get('getSchoolSpec/{id}', [SchoolController::class, 'getSchoolSpecBySchool']);
        Route::get('getSchoolPlan/{id}', [SchoolController::class, 'getSchoolPlan']);
        // 获取招生简章
        Route::get('getNews', [SchoolController::class, 'getNews']);
        // 获取招生资讯
        Route::get('getInfo', [SchoolController::class, 'getInfo']);
        Route::get('getArticle/{id}', [SchoolController::class, 'article']);
        // 根据id获取专业
        Route::get('getSpec/{id}', [SchoolController::class, 'getSpecById']);
    });

    Route::prefix('netCourse')->group(function () {
        Route::get('getInfo', [NetCourseController::class, 'getInfo']);
        Route::get('getCourseColumnInfo', [CourseColumnController::class, 'getInfo']);
        Route::get('getCourseColumnGroupBuyList', [CourseColumnController::class, 'getCourseColumnGroupBuyList']);

        // 开团
        Route::post('storeCourseColumnGroupBuy', [GroupBuyController::class, 'store'])->middleware(['auth:api']);
        // 凑团
        Route::post('joinCourseColumnGroupBuy', [GroupBuyController::class, 'join'])->middleware(['auth:api']);

        // 获取视频课程详情
        Route::get('getLearnVideoInfo', [LearnVideoController::class, 'getLearnVideoInfo']);
        Route::get('getLearnVideoChapters', [LearnVideoController::class, 'getLearnVideoChapters']);

        // 保存视频播放进度
        Route::post('savePlayDuration', [LearnVideoController::class, 'savePlayDuration'])->middleware(['auth:api']);
    });

    // 获取文章
    Route::get('getArticle/{id}', [ArticleController::class, 'show']);


    // 获取文档首页
    Route::get('documentCommon', [DocumentController::class, 'common']);
    Route::get('documents', [DocumentController::class, 'list']);
    Route::get('document/{id}', [DocumentController::class, 'show']);


    Route::middleware(['auth:api', 'checkUserStatus'])->group(function () {
        // 检测用户是否拥有该题的访问权限（可以单题或根据知识点来判断）
        Route::get('userHaveQuestionAuthCheck', [UserController::class, 'userHaveQuestionAuthCheck']);

        // 获取我购买的文档
        Route::get('getMyBuyDocuments', [DocumentController::class, 'getMyBuyDocuments']);

        // 获取我收藏的文档
        Route::get('getMyCollectDocuments', [DocumentController::class, 'getMyCollectDocuments']);

        // 获取我的文档订单
        Route::get('getMyDocumentOrders', [DocumentController::class, 'getMyDocumentOrders']);

        // 购买文档
        Route::post('documentBuy', [DocumentController::class, 'buy']);
        // 收藏/取消收藏文档
        Route::post('documentCollectSwitch/{id}', [DocumentController::class, 'collectSwitch']);
        // 下载文档
        Route::get('downloadDocument/{id}', [DocumentController::class, 'downloadDocument']);
        // 统计文档数据
        Route::post('documentStatic', [DocumentController::class, 'documentStatic']);


        // 通用
        Route::prefix('common')->group(function () {
            // Route::get('getRank', [\App\Http\Controllers\QH_Api\CommonController::class, 'getRankV2']);
            // Route::get('getKaoshiRank', [\App\Http\Controllers\QH_Api\CommonController::class, 'getKaoshiRank1']);
            Route::get('getKaoshiRank/{knowledgeId}', [\App\Http\Controllers\QH_Api\CommonController::class, 'getKaoshiRank']);
            Route::post('feedback', [CommonController::class, 'feedbackStore']);
        });

        // 关注/取消关注学校
        Route::post('school/follow', [SchoolController::class, 'follow']);

        // 学科
        Route::prefix('course')->group(function () {

        });

        // 知识点
        Route::prefix('knowledge')->group(function () {

        });

        // 用户
        Route::prefix('user')->group(function () {
            Route::get('getInfo', [UserController::class, 'getInfo']);
            Route::post('updateInfo', [UserController::class, 'updateInfo']);
            Route::get('getCollect', [UserController::class, 'getCollect']);
            Route::get('getCuoti', [UserController::class, 'getCuoti']);
            Route::delete('delCollect', [UserController::class, 'delCollect']);
            Route::delete('delCuoti', [UserController::class, 'delCuoti']);
            Route::delete('delKnowledgePost', [UserController::class, 'delKnowledgePost']);
            Route::get('statistics', [UserController::class, 'statistics']);
            Route::get('getNote', [NoteController::class, 'getMyNote']);
            Route::get('getCollectKnowledgePost', [UserController::class, 'getCollectKnowledgePost']);
            // 获取我的题库
            Route::get('getMyQuestionLibrary', [QuestionLibraryController::class, 'getMyQuestionLibrary']);
            // 获取我的兑换记录
            Route::get('getMyQuestionLibraryBuyLogForCdkey', [UserController::class, 'getMyQuestionLibraryBuyLogForCdkey']);
            // 获取我的收藏
            Route::get('getMyCollectQuestion', [UserController::class, 'getMyCollectQuestion']);
            Route::get('getMyCollectQuestion2', [UserController::class, 'getMyCollectQuestionV2']);
            // 获取我的错题
            Route::get('getMyCuoti', [UserController::class, 'getMyCuoti']);
            Route::get('getMyCuoti2', [UserController::class, 'getMyCuotiV2']);
            Route::delete('deleteUserQuestion', [UserController::class, 'deleteUserQuestion']);
            // 获取我的笔记
            Route::get('getMyNote', [UserController::class, 'getMyNote']);
            // 删除我的笔记
            Route::get('deleteMyNote/{id}', [UserController::class, 'deleteMyNote']);
            // 获取我的关注院校
            Route::get('getMyFollowSchool', [UserController::class, 'getMyFollowSchool']);
            // 获取我的优惠券
            Route::get('getCoupons', [UserController::class, 'getCoupons']);
            // 领取优惠券
            Route::post('storeCoupon', [UserController::class, 'storeCoupon']);
            // 获取我的视频课程
            Route::get('getMyLearnVideo', [LearnVideoController::class, 'getMyLearnVideo']);
            // 获取我的视频课程购买记录
            Route::get('getMyLearnVideoBuyLog', [LearnVideoController::class, 'getMyLearnVideoBuyLog']);
            Route::get('getNetCourseCdekyUserLog', [UserController::class, 'getNetCourseCdekyUserLog']);
            // 取消订单
            Route::post('cancelOrder', [UserController::class, 'cancelOrder']);
            // 添加课程限免记录
            Route::post('learnVideoLimitFreeStore', [UserController::class, 'learnVideoLimitFreeStore']);
        });

        Route::prefix('questions')->group(function () {
            // 纠错
            Route::post('report', [QuestionsController::class, 'report']);
            // 获取笔记
            Route::get('getNoteByQuestion/{id}', [NoteController::class, 'getNoteByQuestionId']);
            // 获取评论
            Route::get('getCommentByQuestion/{id}', [CommentController::class, 'getCommentByQuestionId']);
            // 收藏
            Route::post('collect', [QuestionController::class, 'collect']);
            // 记录刷题
            Route::post('learn', [QuestionController::class, 'learn']);
        });
        // 答题结果汇报
        Route::post('answerResultCheck', [QuestionController::class, 'answerResultCheck']);
        Route::post('answerResultCheckOfKaoshi', [QuestionController::class, 'answerResultCheckOfKaoshi']);
        Route::post('answerResultCheckOfLit', [QuestionController::class, 'answerResultCheckOfLit']);

        // 清空小结的答题记录
        Route::post('clearQuestionLogByKnowledge/{id}', [QuestionController::class, 'clearQuestionLogByKnowledge']);

        // 清空指定ID的答题记录
        Route::post('clearQuestionLogByIds', [QuestionController::class, 'clearQuestionLogByIds']);

        // 考试结果汇报
        Route::post('kaoshiResultCheck', [QuestionController::class, 'kaoshiResultCheck']);
        Route::post('reKaoshi', [QuestionController::class, 'reKaoshi']);
        Route::get('getKaoshiResult/{id}', [QuestionController::class, 'getKaoshiResult']);

        // 使用激活码激活题库
        Route::post('useCdkey', [QuestionLibraryController::class, 'useCdkey']);
        // 使用激活码激活网课
        Route::post('useCdkey2', [QuestionLibraryController::class, 'useCdkey2']);

        // 笔记
        Route::prefix('note')->group(function () {
            Route::post('', [NoteController::class, 'store']);
            Route::delete('{id}', [NoteController::class, 'delete']);
            Route::post('like/{id}', [NoteController::class, 'like']);
        });

        // 评论
        Route::prefix('comment')->group(function () {
            Route::post('', [CommentController::class, 'store']);
            Route::delete('{id}', [CommentController::class, 'delete']);
            Route::post('like/{id}', [CommentController::class, 'like']);
        });

        // 订单
        Route::prefix('order')->group(function () {
            Route::get('', [\App\Http\Controllers\QH_Api\OrderController::class, 'list']);
            Route::get('{id}', [\App\Http\Controllers\QH_Api\OrderController::class, 'show']);

            // 下单
            Route::post('store', [\App\Http\Controllers\QH_Api\OrderController::class, 'store']);
        });
        // 获取商品信息
        Route::get('getGoodsInfo', [\App\Http\Controllers\QH_Api\OrderController::class, 'getGoodsInfo']);

        // 获取订单支付信息
        Route::get('getOrderPaymentInfo/{id}', [\App\Http\Controllers\QH_Api\OrderController::class, 'getOrderPaymentInfo']);

        // 下单
        Route::post('submitOrder', [\App\Http\Controllers\QH_Api\OrderController::class, 'submitOrder'])->middleware(['orderRequestRateLimit']);
        Route::post('submitOrder2', [\App\Http\Controllers\QH_Api\OrderController::class, 'submitOrder2'])->middleware(['orderRequestRateLimit']);
        // 获取下单前置信息
        Route::get('getOrderSubmitPreInfo/{id}', [\App\Http\Controllers\QH_Api\OrderController::class, 'getOrderSubmitPreInfo']);
        Route::get('getOrderSubmitPreInfo2/{id}', [\App\Http\Controllers\QH_Api\OrderController::class, 'getOrderSubmitPreInfo2']);

    });
    // 微信支付结果通知
    Route::any('wechatNotify', [\App\Http\Controllers\QH_Api\OrderController::class, 'wechatNotify']);
    // 微信退款结果通知
    Route::any('wechatRefundNotify', [\App\Http\Controllers\QH_Api\OrderController::class, 'wechatRefundNotify']);

    // 文档-微信支付结果通知
    Route::any('documentWechatNotify', [DocumentController::class, 'wechatNotify']);
    // 文档-微信退款结果通知
    Route::any('documentWechatRefundNotify', [DocumentController::class, 'wechatRefundNotify']);
});
